/*
 * 文 件 名:  DateUtil.java
 * 版    权:  Isoftstone Technologies Co., Ltd. Copyright 2010,  All rights reserved
 * 描    述:  日期工具类
 * 修 改 人:  yuma
 * 修改时间:  Aug 9, 2010
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.iss.umservice.common;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;

/**
 * 
 * <p>
 * [功能简述] 日期工具类
 * <p>
 * <p>
 * [详细描述] 日期工具类
 * </p>
 * @author  yuma
 * @version  [GZT 1.00, Aug 9, 2010]
 * @see  [相关类/方法]
 * @since  [GZT]
 */
public class DateUtil
{
    /** 日志对象 */
    private static final Logger log = Logger.getLogger(DateUtil.class);
    
    public static boolean canrun = false;
    
    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * 默认构造函数
     */
    private DateUtil()
    {
    }
    
    /**
     * 得到当前时间的字符格式
     * @return string
     */
    public static String getCurrentTime()
    {   
        SimpleDateFormat standardFormate;
        standardFormate = new SimpleDateFormat(STANDARD_FORMAT, Locale.ENGLISH);
        return standardFormate.format(new Date());
    }
    
    /**
     * 得到当前时间的字符格式
     * @return string
     */
    public static String getCurrentTime(String format)
    {
        SimpleDateFormat standardFormate;
        standardFormate = new SimpleDateFormat(format, Locale.ENGLISH);
        return standardFormate.format(new Date());
    }
    
    /**
     * 得到当前时间
     * @return timeStamp
     */
    public static Timestamp getCurrentDatetime()
    {
        return new Timestamp(System.currentTimeMillis());
    }
    
    /**
     * 格式化成系统常用日期格式：yyyyMMddHHmmss
     * @param date
     *          Date
     * @return string
     */
    public static String format(final Date date)
    {   
        if (date == null)
        {
            return null;
        }
        SimpleDateFormat standardFormate;
        standardFormate = new SimpleDateFormat(STANDARD_FORMAT, Locale.ENGLISH);
        return standardFormate.format(date);
    }
    
    /**
     * 格式化日期
     * @param date
     *          Date
     * @param formatStr
     *          FormatStr         
     * @return string.
     */
    public static String format(final Date date, final String formatStr)
    {   
        SimpleDateFormat simpledf;
        if (date == null)
        {
            return "";
        }
        
        simpledf = new SimpleDateFormat(formatStr,Locale.ENGLISH);
        return simpledf.format(date);
    }
    
    /**
     * 格式化日期
     * @param date
     *          Date
     * @param formatStr
     *          FormatStr         
     * @return string.
     */
    public static String format(final Timestamp date, final String formatStr)
    {   
        SimpleDateFormat simpledf;
        if (date == null)
        {
            return "";
        }
        
        simpledf = new SimpleDateFormat(formatStr,Locale.ENGLISH);
        return simpledf.format(date);
    }
    
    /**
     * 判断两个日期是否为同年同月.
     * @param date1
     *          date1
     * @param date2
     *          date2
     * @return boolean                  
     */
    public static boolean isSameYYYYMM(final Date date1,final Date date2)
    {
        Calendar calendar1;
        calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        
        Calendar calendar2;
        calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        
        return (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR))
            && (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH));
    }
    
    /**
     * 把字符串格式化日期
     * @param dateStr
     *          date
     * @param formater
     *          format     
     *  @return Date.            
     */
    public static Date formdate(final String dateStr, final String formater)
    {   
        String formaterr = "";
        formaterr = (null == formater) ? "yyyy-MM-dd HH:mm:ss" : formater;
        DateFormat dataFormat;
        dataFormat = new SimpleDateFormat(formaterr,Locale.ENGLISH);
        Date date = null;
        try
        {
            date = dataFormat.parse(dateStr);
        }
        catch (ParseException e)
        {
            log.error(e.getMessage());
            
        }
        return date;
    }
    
    // 从传入的年月换算得到开始结束时间
    public static String[] changedate(String countMonth)
    {   
        String[] re = new String[2];
        if (countMonth != null && !"".equals(countMonth))
        {
            // 获取月份 原格式2010-03
            String month = countMonth.substring(countMonth.lastIndexOf("-") + 1);
            // 获取年份
            String year = countMonth.substring(0, countMonth.lastIndexOf("-"));
            String startDay = "01"; // 开始时间
            
            String endDay = null;
            // 结束时间
            int tempMonth = Integer.valueOf(month);
            if (tempMonth == 1 || tempMonth == 3 || tempMonth == 5
                    || tempMonth == 7 || tempMonth == 8 || tempMonth == 10
                    || tempMonth == 12)
            {
                endDay = "31";
            }
            if (tempMonth == 4 || tempMonth == 6 || tempMonth == 9
                    || tempMonth == 11)
            {
                endDay = "30";
            }
            if (tempMonth == 2)
            {
                if (isLeapYear(Integer.valueOf(year)))
                {
                    endDay = "29";
                }
                else
                {
                    endDay = "28";
                }
            }
            
            // 开始时间和结束时间
            StringBuffer beginTime = new StringBuffer();
            StringBuffer endTime = new StringBuffer();
           
            beginTime.append(year);
            beginTime.append("-");
            beginTime.append(month);
            beginTime.append("-");
            beginTime.append(startDay);
            beginTime.append(" 00:00:00");
            
            endTime.append(year);
            endTime.append("-");
            endTime.append(month);
            endTime.append("-");
            endTime.append(endDay);
            endTime.append(" 23:59:59");
            
            re[0] = beginTime.toString();
            re[1] = endTime.toString();
        }
        return re;
    }
    
    /**
     * 判断是否是闰年

     * <p>
     * [功能简述]
     * <p>
     * <p>
     * [详细描述]
     * </p>
     * 
     * @param yearNum
     * @return [参数说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static boolean isLeapYear(int yearNum)
    {
        boolean isLeep = false;
        /** 判断是否为闰年，赋值给一标识符flag */
        if ((yearNum % 4 == 0) && (yearNum % 100 != 0))
        {
            isLeep = true;
        }
        else if (yearNum % 400 == 0)
        {
            isLeep = true;
        }
        else
        {
            isLeep = false;
        }
        return isLeep;
    }
    
    /**
     * 转换时间格式为MMdd
     * 
     * @param dateString
     *          Date
     * @return string
     * @see [类、类#方法、类#成员]
     */
    public static String transDateMMdd(final String dateString)
    {   
        Date date;
        try
        {
            date = StringUtil.timeStr2Date(dateString, "yyyyMMddHHmmss");
            return StringUtil.date2TimeStr(date, "MMdd");
        }
        catch (Exception e)
        {
            return "";
        }
    }
    
    /**
     * 日期转换 <功能详细描述>
     * 
     * @param noFormat
     *      format.
     * @return Sting
     * @see [类、类#方法、类#成员]
     */
    public static String transDate(final String noFormat)
    {   
        Date date;
        try
        {
            date = StringUtil.timeStr2Date(noFormat, "yyyyMMddHHmmss");
            return StringUtil.date2TimeStr(date, "yyyy-MM-dd HH:mm:ss");
        }
        catch (Exception e)
        {
            return "";
        }
    }
    
    /**
     * 日期转换成“yyyy-MM-dd”格式<一句话功能简述> <功能详细描述>
     * 
     * @param noFormat
     *          format.
     * @return str
     * @see [类、类#方法、类#成员]
     */
    public static String transDate2(final String noFormat)
    {   
        Date date;
        try
        {
            date = StringUtil.timeStr2Date(noFormat, "MM/dd/yyyy");
            
            return StringUtil.date2TimeStr(date, "yyyy年MM月dd日");
        }
        catch (Exception e)
        {
            return "";
        }
    }
    
    /**
     * 判断一个字符串是否满足给定的日期格式<一句话功能简述> <功能详细描述>
     * 
     * @param str
     *          String
     * @param dateFormat
     *          Dateformat.
     * @return boolean
     * @see [类、类#方法、类#成员]
     */
    public static boolean judgeDateFormat(final String str,final  String dateFormat)
    {   
        SimpleDateFormat formatter;
        if (str != null)
        {
            formatter = new SimpleDateFormat(dateFormat,Locale.ENGLISH);
            formatter.setLenient(false);
            
            try
            {
                formatter.format(formatter.parse(str));
            }
            catch (Exception e)
            {
                return false;
            }
            
            return true;
        }
        
        return false;
    }
}
